#include <16f876.h>
#fuses XT,NOWDT
#use delay(clock=4000000)
#include <lcd.c>
#byte PIR1=0x0C


float AP=0.0;         //Valor final del ancho de pulso
int1 nuevopulso=0;

long rise,fall;
int16 TF=0;
#int_ccp2
void isr()
{
   rise = CCP_1;
   fall = CCP_2;
   nuevopulso=1; 
        // CCP_1 is the time the pulse went high
}                                 // CCP_2 is the time the pulse went low
                                  // pulse_width/(clock/4) is the time

                                  // In order for this to work the ISR
                                  // overhead must be less than the
                                  // low time.  For this program the
                                  // overhead is 45 instructions.  The
                                  // low time must then be at least
                                  // 9 us.

void main()
{
 
   setup_ccp1(CCP_CAPTURE_RE);    // Configure CCP1 to capture rise
   setup_ccp2(CCP_CAPTURE_FE);    // Configure CCP2 to capture fall
   setup_timer_1(T1_INTERNAL);    // Start timer 1

   enable_interrupts(INT_CCP2);   // Setup interrupt on falling edge
   enable_interrupts(GLOBAL);

    do {
    if(nuevopulso==1){     //Pulso nuevo?
       TF=(rise-fall);   //Ancho de pulso.
       AP = TF*1.0;      //Ancho de pulso en microsegundos (a 4MHz:1us)
      printf(lcd_putc,"\nPulso = %6.1fuS ", AP);
     nuevopulso=0;             //Pulso ya medido, espera nuevo
   }
  } while (TRUE);
}
